/*
 * Created by JFormDesigner on Sat Jun 25 13:43:40 CST 2022
 */

package jw3.c10.view;

import java.awt.event.*;

import jw3.c10.model.Role;
import jw3.c10.utils.DBConnection;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class RoleA extends JFrame {
    public RoleA() {
        initComponents();
        this.setSize(800,800);
//        table1.setSize(600,400);
//        panel1.setSize(600,400);
        this.setVisible(true);
    }
    public void showData(String name){
        String sql="SELECT * FROM role where  RName like  ? ";
        try {
            Connection conn= DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,"%"+name+"%");
            ResultSet rs=ps.executeQuery();
            data=new Vector<Vector<Object>>();
            while (rs.next()){
                Vector<Object> data1=new Vector<Object>();
                data1.add(rs.getInt("RID"));
                data1.add(rs.getString("RName"));
                data.add(data1);
            }
            DBConnection.close(rs,ps,conn);
            Vector<String> heads=new Vector<String>();
            heads.add("线编号");
            heads.add("线路名称");
            DefaultTableModel dtm=new DefaultTableModel(data,heads);
            table1.setModel(dtm);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
        //增加
        dialog1.setModal(true);
        dialog1.setVisible(true);
    }

    private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here
        Role role=new Role();
        role.setRName(textField2.getText());
        String sql="insert role values(0,?)";
        try {
            Connection conn=DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,role.getRName());
            if (ps.executeUpdate()>0){
                showData("");
                JOptionPane.showMessageDialog(null,"添加成功");
            }else {
                JOptionPane.showMessageDialog(null,"添加失败");

            }
            DBConnection.close(null,ps,conn);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
//        删除

        int index=table1.getSelectedRow();//获取索引
        if (index!=-1){
            if (JOptionPane.showConfirmDialog(null,"是否确定删除？")==0){
                String sql="delete from role where RID=?";
                int id= (int) data.get(index).get(0);
                try {
                    Connection conn=DBConnection.getConnection();
                    PreparedStatement ps=conn.prepareStatement(sql);
                    ps.setInt(1,id);
                    if (ps.executeUpdate()>0){
                        showData("");
                        JOptionPane.showMessageDialog(null,"删除成功");
                    }else {
                        JOptionPane.showMessageDialog(null,"删除失败");
                    }




                } catch (Exception ex) {
                    ex.printStackTrace();
                }


            }
        }else {
            JOptionPane.showMessageDialog(null,"您未选中任意一行");
        }
    }

    private void button6MouseClicked(MouseEvent e) {
        // TODO add your code here

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        textField1 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button6 = new JButton();
        dialog1 = new JDialog();
        label1 = new JLabel();
        textField2 = new JTextField();
        button4 = new JButton();
        button5 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(textField1);
        textField1.setBounds(100, 20, 110, textField1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u641c\u7d22");
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(5, 20), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u589e\u52a0");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(220, 20, 78, 30);

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3);
        button3.setBounds(315, 20, 78, 30);

        //======== panel1 ========
        {
            panel1.setLayout(new GridLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(40, 110), panel1.getPreferredSize()));

        //---- button6 ----
        button6.setText("text");
        button6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button6MouseClicked(e);
            }
        });
        contentPane.add(button6);
        button6.setBounds(new Rectangle(new Point(410, 20), button6.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);

            //---- label1 ----
            label1.setText("\u7ad9\u70b9\u540d\u79f0");
            dialog1ContentPane.add(label1);
            label1.setBounds(new Rectangle(new Point(35, 35), label1.getPreferredSize()));
            dialog1ContentPane.add(textField2);
            textField2.setBounds(110, 30, 70, textField2.getPreferredSize().height);

            //---- button4 ----
            button4.setText("\u786e\u5b9a");
            button4.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button4MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button4);
            button4.setBounds(new Rectangle(new Point(40, 105), button4.getPreferredSize()));

            //---- button5 ----
            button5.setText("\u53d6\u6d88");
            dialog1ContentPane.add(button5);
            button5.setBounds(165, 105, 78, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        showData("");
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTextField textField1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button6;
    private JDialog dialog1;
    private JLabel label1;
    private JTextField textField2;
    private JButton button4;
    private JButton button5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    private  static Vector<Vector<Object>> data;

}
